home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT12 / FSOUND.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-05-10  |  4.4 KB  |  88 lines

  1. ;
  2. ;       Program FSound ( Chapter 12 )
  3. ;
  4.     page    60,132
  5. .model  large,FORTRAN
  6. ;===    This generates the statement PROTO for MASM 6.0 or PUBLIC for others
  7.     IFDEF ??VERSION
  8. public  FSOUND  
  9.     ELSEIF @version EQ 600
  10. Fsound  PROTO    FORTRAN freq: FAR PTR WORD, durat: FAR PTR WORD
  11.     ELSE
  12. public  FSOUND
  13.     ENDIF   
  14. ;===    Data segments   
  15. .data                   
  16. Nticks  dw      0                       ; number of ticks for delaying
  17. ;===    Code segment
  18. .code
  19. Fsound  PROC    FORTRAN uses ax bx cx dx es di, freq: FAR PTR WORD,
  20.         durat: FAR PTR WORD
  21. ;===    accept the parameter DURAT (sound duration)
  22.     mov     ax,5000                 ; default value for DURAT is 5 seconds
  23.     les     bx,durat                ; address of DURAT into ES:BX
  24.     mov     bx,es:[bx]              ; value of DURAT into BX register
  25.     cmp     bx,0                    ; compare DURAT to 0
  26.     je      Accept                  ; skip illegal value of DURAT
  27.     cmp     ax,5000                 ; compare DURAT to 5000
  28.     jg      Accept                  ; skip illegal value of DURAT
  29.     mov     ax,bx                   ; load DURAT into AX
  30. ;===    convert DURAT value into timer ticks ( Tics = Msecs * 91 / 5000)
  31. Accept: mov     Nticks,ax               ; save value of DURAT in memory
  32. ;===    modify the latch of the timer channel 0 (10 times faster)       
  33.     mov     al,00110110b
  34.     out     43h,al
  35.     mov     ax,1193                 ; latch value - 1/10 of generator freq.
  36.     out     40h,al                  ; send low byte of latch value
  37.     mov     al,ah                   ; prepare for sending high byte 
  38.     out     40h,al                  ; send high byte of latch value
  39. ;===    accept the parameter FREQ (sound frequency)
  40.     les     bx,freq                 ; address of frequency into ES:BX
  41.     mov     di,es:[bx]              ; value of frequency into DI
  42.     cmp     di,0                    ; is zero frequency reqested?
  43.     jg      Sound                   ; if not, generate sound
  44. ;===    zero frequency - disable sound  
  45.     in      al,61h                  ; read speaker port content
  46.     and     al, not 00000011b       ; set bits 0 and 1 of port 61h to 1     
  47.     out     61h,al                  ; turn speaker off
  48.     jmp     ToTicks                 ; wait for time defined by DURAT
  49. ;===    program channel 2 of Programmable Timer for sound generation    
  50. Sound:  mov     al,10110110b            ; channel 2, write lsb/msb,
  51.     out     43h,al                  ;   operation mode 3, binary
  52.     mov     dx,12h                  ; store 12 34DCh (1 193 180) into
  53.     mov     ax,34dch                ;    DX:AX for DIV command (divident)
  54.     div     di                      ; obtain frequency divisor
  55.     out     42h,al                  ; send low byte of divisor
  56.     mov     al,ah                   ; prepare for sending high byte
  57.     out     42h,al                  ; send high byte of divisor
  58. ;===    turn the sound on       
  59.     in      al,61h                  ; read speaker port content
  60.     or      al,00000011b            ; set bits 0 and 1 of port 61h to 1
  61.     out     61h,al                  ; turn speaker on
  62. ;===    get current time (the number of ticks since midnight)
  63. ToTicks:mov     ax,40h                  ; address of BIOS data segment into AX 
  64.     mov     es,ax                   ; ES will point to BIOS data segment
  65. ;===    calculate the moment of turning the sound off           
  66.     mov     bx,es:[6Ch]             ; low part of ticks number into BX
  67.     add     bx,Nticks               ; add DURAT to that low part value
  68.     mov     dx,es:[6Eh]             ; save high part of ticks number
  69. ;===    wait for the obtained number of ticks defined by the DURAT parameter    
  70. Delay:  cmp     es:[6Eh],dx             ; has high part of time counter changed?
  71.     jne     IsTime                  ; if so, suppose that time has gone
  72.     cmp     es:[6Ch],bx             ; has time gone?        
  73.     jb      Delay                   ; if not, continue to wait
  74. ;==     turn the speaker off    
  75. IsTime: in      al,61h                  ; read speaker port contens
  76.     and     al, not 00000011b       ; set bits 0 and 1 of port 61h to 1     
  77.     out     61h,al                  ; turn speaker off
  78. ;===    restore the latch of the timer channel 0 (default value is 0FFFFh)
  79.     mov     al,00110110b
  80.     out     43h,al
  81.     mov     al,0FFh                 ; this is low byte of value 65535
  82.     out     40h,al                  ; send low byte of latch value (65535)
  83.     out     40h,al                  ; send high byte of latch value (65535)
  84. ;===    return to caller                
  85.     ret
  86. fsound  endp
  87.     end
  88.